/**
 * Copyright (C), 2018 store
 * Encoding: UTF-8
 * Date: 20-4-15 下午12:32
 * History:
 */
package com.swak.frame.config;

import freemarker.template.TemplateModelException;
import freemarker.template.utility.XmlEscape;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;

import javax.servlet.ServletContext;
import java.util.List;

/**
 * SwakWebMvcConfigurer.java
 * 
 * @author ColleyMa
 * @date 20-4-15 下午12:32
 * @version v1.0
 */
public  class SwakWebMvcConfigurer implements SwakMvcConfigurer {

	@Autowired(required = false)
	private freemarker.template.Configuration configuration;

	@Autowired
	private ServletContext servletContext;

	@Override
	public void configureSwakResolver(ViewResolverRegistry registry) {
		if (configuration != null) {
			// 自定义配置信息
			try {
				this.configuration.setSharedVariable("ctx", this.servletContext.getContextPath());
				this.configuration.setSharedVariable("xml_escape", new XmlEscape());
				//this.configuration.setSharedVariable("shiro", new ShiroTags());
			} catch (TemplateModelException e) {
				// ingro
			}
		}
	}

	@Override
	public void configureViewResolvers(ViewResolverRegistry registry) {
		configureSwakResolver(registry);
	}


	@Override
	public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
		for (HttpMessageConverter<?> messageConverter : converters) {
			System.out.println(messageConverter);
		}
	}
}
